package com.itheima.app.controller;

import cn.hutool.core.bean.BeanUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.UserManager;
import com.itheima.domain.db.User;
import com.itheima.domain.db.UserInfo;
import com.itheima.vo.UserInfoVo;
import com.itheima.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;



@RestController
public class UserController {

    @Autowired
    private UserManager userManager;

   /* @PostMapping("/user")//接受前端参数
    public Long save(@RequestBody User user){

        //调用manager保存方法
        Long id = userManager.save(user);

        //返回主键
        return  id;
    }

    @GetMapping("/user")//接受前端参数
    public User findByPhone(String phone){

        //调用manager按手机号查用户方法
        User user = userManager.findByPhone(phone);

        //返回查询到的用户信息
        return user;
    }*/

    //发送手机验证码
    @PostMapping("/user/login") //前端传过来的是json，转换后用map存储
    public void sendSmsCode(@RequestBody Map<String,String> map){

        //接收参数
        String phone = map.get("phone");
        System.out.println(phone);

        //调用manager
        userManager.sendSmsCode(phone);
    }

    //登录与注册
    @PostMapping(value = "/user/loginVerification",name = "0101")
    public Map<String,Object> resultMap(@RequestBody Map<String,String> map){

        //接收参数
        //手机号
        String phone = map.get("phone");
        //验证码
        String verificationCode = map.get("verificationCode");

        //调用manager  并返回结果
        return userManager.loginOrReg(phone,verificationCode);
    }

    //完善个人基本信息
    @PostMapping("/user/loginReginfo")
    public void saveUserBaseInfo(@RequestBody UserInfo userInfo){
            //1. 解析token
            User user = UserHolder.get();
            //2.向userInfo设置userId
            userInfo.setId(user.getId());
            //3.调用manager
            userManager.saveUserBaseInfo(userInfo);
    }


    //完善头像信息(注册/更新都走这个方法)
    @PostMapping({"/user/loginReginfo/head","/users/header"})
    public void saveUserHeaderInfo(MultipartFile headPhoto) throws IOException {
        //1. 获取token中的userId
        Long userId = UserHolder.get().getId();
        //2. 调用manager
        userManager.saveUserHeaderInfo(userId, headPhoto);
    }


    //查询个人信息
    @GetMapping("/users")
    public UserInfoVo findUserInfo(@RequestParam(value = "userID",required = false) Long userId,
                                   @RequestParam(value = "huanxinID",required = false)Long huanxinId
                                  )
    {
        //如果userID不为空,就按照userID查询
        if(userId != null){
            return userManager.findUserById(userId);
        }else if(huanxinId != null){
            //如果userID为空,huanxinID不为空,就按照huanxinID查询
            return userManager.findUserById(huanxinId);
        }else{
            //如果上面两个都为空,就从token中获取登录用户id查询
            Long uid = UserHolder.get().getId();
            return userManager.findUserById(uid);
        }
    }


    //更新个人基本信息
    @PutMapping("/users")
    public void updateUserBaseInfo(@RequestBody UserInfo userInfo){
        userManager.updateUserBaseInfo(userInfo);
    }



    private User getUserFromToken(String token) {

        Map map = JwtUtil.parseToken(token);

        User user = BeanUtil.mapToBean(map, User.class, true);

        return user;
    }


}
